package cn.yaxma.xmpp;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Yaxma on 2016/11/1.
 * Email：2678503498@qq.com
 */

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int RECEIVE = 0;
    private static final int SEND = 1;

    private Context mContext;
    private List<ChatMessage> chatList = new ArrayList<>();

    public ChatAdapter(Context context) {
        mContext = context;
    }

    public void addMessage(ChatMessage chatMessage) {
        if (chatMessage != null) {
            chatList.add(chatMessage);
            notifyDataSetChanged();
        }
    }

    @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == RECEIVE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_receive, parent, false);
            return new ReceiveViewHolder(view);
        } else if (viewType == SEND) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_send, parent, false);
            return new SendViewHolder(view);
        }
        return null;
    }

    @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ChatMessage chatMessage = chatList.get(position);
        if (holder instanceof ReceiveViewHolder) {
//            Glide.with(mContext).load(chatMessage.getHeadUrl()).into(((ReceiveViewHolder) holder).headIv);
            ((ReceiveViewHolder) holder).msgTv.setText(chatMessage.getMsg());
        } else if (holder instanceof SendViewHolder) {
//            Glide.with(mContext).load(chatMessage.getHeadUrl()).into(((SendViewHolder) holder).headIv);
            ((SendViewHolder) holder).msgTv.setText(chatMessage.getMsg());
        }
    }

    @Override public int getItemCount() {
        return chatList == null ? 0 : chatList.size();
    }

    @Override public int getItemViewType(int position) {
        ChatMessage chatMessage = chatList.get(position);
        if (chatMessage.getDirection() == ChatMessage.Direction.RECEIVE) {
            return RECEIVE;
        } else {
            return SEND;
        }
    }

    class ReceiveViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView headIv;
        private TextView msgTv;

        public ReceiveViewHolder(View itemView) {
            super(itemView);
            headIv = (CircleImageView) itemView.findViewById(R.id.head_civ);
            msgTv = (TextView) itemView.findViewById(R.id.message_tv);
        }
    }

    class SendViewHolder extends RecyclerView.ViewHolder {

        private CircleImageView headIv;
        private TextView msgTv;

        public SendViewHolder(View itemView) {
            super(itemView);
            headIv = (CircleImageView) itemView.findViewById(R.id.head_civ);
            msgTv = (TextView) itemView.findViewById(R.id.message_tv);
        }
    }

}
